/**************************************************************
  > File Name: 122.c
  > Author: LuoLiang
  > Mail: 1204553475@qq.com 
  > Created Time: 2022年02月10日 星期四 19时41分38秒
 **************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int maxProfit(int* prices, int pricesSize)
{
	int i;
	int buy = 0;
	int money = 0;
	int oldmoney;
	
	for (i = 0; i < pricesSize; i++)	
	{
		if (buy == 0 && ((i - 1 < 0 || prices[i] <= prices[i-1]) && (i + 1 >= pricesSize || prices[i] <= prices[i+1])))
		{
			buy = 1;
			oldmoney = money;
			money -= prices[i];
		}
		else if (buy == 1 && ((i - 1 < 0 || prices[i] >= prices[i-1]) && (i + 1 >= pricesSize || prices[i] >= prices[i+1])))
		{
			buy = 0;
			money += prices[i];
		}
	}
	if (buy == 1)
		return oldmoney;
	return money;
}

int main(int argc, char **argv)
{
	int arr[] = {7,1,5,3,6,4};
	int ret;

	ret = maxProfit(arr, 6);
	printf("%d\n",ret);

    exit(0);
}
